﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TomatoClock.Services;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;

namespace TomatoClock.Views
{
    public partial class BackgroundControl : UserControl
    {
        BackgroundManager backgroundManager = new BackgroundManager();
        public BackgroundControl()
        {
            InitializeComponent();
            LoadBackgrounds();
        }
        private void LoadBackgrounds()
        {
            lbBackgrounds.Items.Clear();
            foreach (var path in backgroundManager.GetBackgroundPaths())
            {
                MaterialSkin.MaterialListBoxItem item = new MaterialSkin.MaterialListBoxItem(path);
                lbBackgrounds.Items.Add(item);
            }
        }
        private void btnImport_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog())
            {
                ofd.Filter = "Image Files (*.jpg;*.jpeg;*.png;*.bmp)|*.jpg;*.jpeg;*.png;*.bmp";
                ofd.Multiselect = true;
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    foreach (string path in ofd.FileNames)
                    {
                        backgroundManager.AddBackgroundPaths(path);
                        MaterialSkin.MaterialListBoxItem item = new MaterialSkin.MaterialListBoxItem(path);
                        lbBackgrounds.Items.Add(item);
                    }
                    // 保存背景路径
                    backgroundManager.SaveBackgroundPaths();
                }
            }
        }
        private void btnRemove_Click(object sender, EventArgs e)
        {
            while (lbBackgrounds.SelectedItems.Count > 0)
            {
                string path = lbBackgrounds.SelectedItem.ToString();
                backgroundManager.RemoveBackgroundPaths(path);
                MaterialSkin.MaterialListBoxItem item = new MaterialSkin.MaterialListBoxItem(path);
                lbBackgrounds.Items.Remove(item);
            }
            // 保存背景路径
            backgroundManager.SaveBackgroundPaths();
        }
        private void lbBackgrounds_SelectedIndexChanged(object sender, MaterialSkin.MaterialListBoxItem selectedItem)
        {
            if (lbBackgrounds.SelectedItem != null)
            {
                string path = lbBackgrounds.SelectedItem.ToString();
                pbPreview.Image = Image.FromFile(path); // 预览选择的图片
            }
        }
        private void btnSetAsBackground_Click(object sender, EventArgs e)
        {
            if (lbBackgrounds.SelectedItem != null)
            {
                string path = lbBackgrounds.SelectedItem.ToString();
                OnBackgroundSelected(path);
            }
        }
        public event EventHandler<string> BackgroundSelected;
        protected virtual void OnBackgroundSelected(string path)
        {
            BackgroundSelected?.Invoke(this, path);
        }


        private void pbPreview_Click(object sender, EventArgs e)
        {

        }
    }
}
